﻿using System;

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using System.Collections.Generic;
using Microsoft.Office.Interop.PowerPoint;

namespace PresentationUtils
{
    class Presentation
    {
        PowerPoint.Application oPPT;
        private PowerPoint.Presentation objPres;

        public delegate void OnPresentationEnd();
        public event OnPresentationEnd PresentationEnd;

        public Presentation(String fileName)
        {
            // Create an instance of PowerPoint.
            oPPT = new PowerPoint.ApplicationClass();
            // Show PowerPoint to the user.
            oPPT.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
            oPPT.Assistant.On = false;
            oPPT.WindowState = PowerPoint.PpWindowState.ppWindowNormal;

            PowerPoint.Presentations objPresSet;
            objPresSet = oPPT.Presentations;

            // Open the presentation
            objPres = objPresSet.Open(fileName,
                Microsoft.Office.Core.MsoTriState.msoFalse,
                Microsoft.Office.Core.MsoTriState.msoTrue,
                Microsoft.Office.Core.MsoTriState.msoTrue);

            objPres.SlideShowSettings.Run();
        }

        public void ClosePresentation()
        {
            if (objPres == null)
                return;

            try
            {
                objPres.Saved = Microsoft.Office.Core.MsoTriState.msoTrue;
                objPres.Close();

                oPPT.WindowState = PowerPoint.PpWindowState.ppWindowMinimized;
            }
            catch (Exception) { }
            finally
            {
                objPres = null;
                if (PresentationEnd != null)
                    PresentationEnd();
            }
        }

        public delegate void PresentationAction();

        private void DoPresentationAction(PresentationAction pa)
        {
            try
            {
                if (objPres.SlideShowWindow.View.State == PpSlideShowState.ppSlideShowDone)
                {
                    ClosePresentation();
                    return;
                }

                pa.Invoke();
            }
            catch (Exception)
            {
                ClosePresentation();
            }
        }

        public void First()
        {
            DoPresentationAction(delegate { objPres.SlideShowWindow.View.First(); });
        }

        public void Last()
        {
            DoPresentationAction(delegate { objPres.SlideShowWindow.View.Last(); });
        }

        public void Next()
        {
            DoPresentationAction(delegate { objPres.SlideShowWindow.View.Next(); });
        }

        public void Previous()
        {

            DoPresentationAction(delegate { objPres.SlideShowWindow.View.Previous(); });
        }

        public String[] SlideNames
        {
            get
            {
                String[] names = new String[objPres.Slides.Count];
                int i = 0;
                foreach (Slide slide in objPres.Slides)
                {
                    names[i++] = slide.Name;
                }
                return names;
            }
        }

        public int NumberOfSlides
        {
            get { return objPres.Slides.Count; }
        }

        public int CurrentSlideNumber
        {
            get
            {
                int nr;
                try
                {
                    nr = objPres.SlideShowWindow.View.CurrentShowPosition;
                }
                catch (Exception) { return 1; }
                return nr;
            }
        }

        public void GoTo(int slideNo)
        {
            DoPresentationAction(delegate
            {
                if (slideNo < 1)
                    return;

                if (slideNo > NumberOfSlides)
                    return;

                objPres.SlideShowWindow.View.GotoSlide(slideNo, Microsoft.Office.Core.MsoTriState.msoFalse);
            });
        }
    }
}
